//
//  CZStatusCell.swift
//  微博08
//
//  Created by apple on 16/3/1.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class CZStatusCell: UITableViewCell {
    
    // 底部按钮的顶部约束: 有可能参照originalView的底部,也有可能参照retweetView的底部
    private var buttomViewTopConstraint: Constraint?
    
    // 属性
    var status: CZStatus? {
        didSet {
            // 当控制器给cell设置status模型时,将模型传递给 originalView
            originalView.status = status
            
            
            // 判断是否要显示 被转发微博内容
            buttomViewTopConstraint?.uninstall()
            if let retweeted_status = status?.retweeted_status {
                // 有转发微博
                retweetView.hidden = false
                
                // 当控制器给cell设置status模型时,将模型传递给 retweetView
                // 注意不要传错模型
                retweetView.status = retweeted_status
                
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.buttomViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            } else {
                retweetView.hidden = true
                
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.buttomViewTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    
    // MARK: - 构造函数
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        prepareUI()
    }
    
    private func prepareUI() {
        // 1.添加子控件
        contentView.addSubview(originalView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetView)
        
        // 2.添加约束
        // 原创微博view约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(10)
            //            make.trailing.equalTo(contentView)
            //            make.height.equalTo(44)
        }
        
        // 被转发微博内容
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
        
        // 底部按钮约束
        bottomView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(44)
            
            self.buttomViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.top.equalTo(self)
            //            make.top.equalTo(self)
            //            make.trailing.equalTo(self)
            
            // contentView的底部
            make.bottom.equalTo(bottomView)
        }
    }
    
    // MARK: - 懒加载
    // 原创微博内容
    private lazy var originalView: CZStatusOriginalView = CZStatusOriginalView()
    
    // 被转发微博内容
    private lazy var retweetView: CYCStatusRetweetView = CYCStatusRetweetView()
    
    // 底部按钮
    private lazy var bottomView: CycStatusBottomView = CycStatusBottomView()
}
